package blogger.client.ui.widgets;

import blogger.client.async.Callback;
import blogger.client.async.services.TwitService;
import blogger.client.async.services.UserService;
import blogger.client.eventqueqe.events.TwitDeletedEvent;
import blogger.client.model.entity.Message;
import blogger.client.model.entity.User;

import com.google.gwt.user.client.ui.*;
import com.google.gwt.user.client.Window;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.HandlerManager;


/**
 * Created by IntelliJ IDEA.
 * User: mega
 * Date: 27.12.2009
 * Time: 2:53:37
 * To change this template use File | Settings | File Templates.
 */
public class TwitView extends Composite{
    private long currentUserId = 0;

    private Message modelMessage = null;

    public User getModelUser() {
        return modelUser;
    }

    public Message getModelMessage() {
        return modelMessage;
    }

    private User modelUser = null;

    private Image image = null;
    private Label login = null;
    private Label text = null;
    private PushButton deleteButton = null;

    public TwitView(User u, Message msg, final HandlerManager hm,long id){
        super();
        currentUserId = id;

        modelMessage = msg;
        modelUser = u;

        HorizontalPanel horizontalPanel = new HorizontalPanel();
        VerticalPanel userView = new VerticalPanel();

        deleteButton = new PushButton(new Image("../images/delete"));
        deleteButton.setSize("16px","16px");
        deleteButton.setStyleName("deleteButton");

        image = new Image("../images/no_foto.gif");
        login = new Label(u.getLogin());
        text = new HTML("<br><br>" + msg.getText() + "<br>");

        userView.add(login);
        userView.add(image);

        VerticalPanel msgView = new VerticalPanel();

        msgView.add(text);
        msgView.add(deleteButton);

        horizontalPanel.add(userView);
        horizontalPanel.add(msgView);
        
        init(hm);
        this.initWidget(horizontalPanel);
    }

    public Image getImage() {
        return image;
    }

    public Label getLogin() {
        return login;
    }
    public void init(final HandlerManager hm){
        UserService.App.getInstance().getCurrentUserId(new Callback<Long>() {
            @Override
            public void doResult(Long result) {
                currentUserId = result;
            }
        });

        deleteButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
//                Window.alert(" " + currentUserId + " ");
                    if(currentUserId == modelMessage.getUser_id()){
                        TwitService.App.getInstance().deleteTwit(modelMessage.getId(),new Callback<Void>() {
                            @Override
                            public void doResult(Void result) {
//                                Window.alert("event");
                                TwitDeletedEvent event = new TwitDeletedEvent(TwitView.this);
                                hm.fireEvent(event);
                            }
                        });
                }
            }
        });
    }
}
